﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Media;

namespace GenTheme.Theme
{
    public class Theme : ITheme
    {
        private IList<IRow> lRows = new List<IRow>();

        public Theme(int width, int length, Type type)
        { 
            if (width > 0 && length > 0)
            {
                bool isShort = false;
                for (int i = 0; i < length; i++)
                {
                    List<Color?> lc = new List<Color?>();
                    for (int j = 0; j < width - (isShort ? 1 : 0); j++)
                    {
                        lc.Add(null);
                    }
                    isShort = !isShort;

                    ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(List<Color?>), typeof(ITheme) });
                    lRows.Add((IRow)ci.Invoke(new object[2] { lc, this }));
                }
            }
        }

        public Theme(List<Color?>[] aLtj, Type type)
        {
            foreach (List<Color?> lc in aLtj)
            {
                ConstructorInfo ci = type.GetConstructor(new Type[] { typeof(List<Color?>), typeof(ITheme) });
                lRows.Add((IRow)ci.Invoke(new object[2] { lc, this }));
            }
        }

        #region ITheme Members

        int ITheme.IndexOf(IRow item)
        {
            if (item != null)
            {
                return lRows.IndexOf(item);
            }
            return -1;
        }

        IRow ITheme.this[int index]
        {
            get
            {
                return this.lRows[index];
            }
        }

        bool ITheme.Contains(IRow item)
        {
            if (item != null)
            {
                return lRows.Contains(item);
            }
            return false;
        }

        int ITheme.Count
        {
            get
            {
                return this.lRows.Count;
            }
        }

        #endregion

        #region IEnumerable<IRow> Members

        IEnumerator<IRow> IEnumerable<IRow>.GetEnumerator()
        {
            return (IEnumerator<IRow>)this.lRows.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.lRows.GetEnumerator();
        }

        #endregion
    }
}
